﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using Licenta_Stomatologie.Forms;

namespace Licenta_Stomatologie.Panels
{
    public partial class PacientSearchPanel : UserControl
    {
        public PacientSearchPanel()
        {
            InitializeComponent();
            filterResults();
        }

        private void filterResults()
        {
            ProjectDBEntities db = new ProjectDBEntities();

            DataTable dt = new DataTable("PacientSearch");

            dt.Columns.AddRange(new DataColumn[] {  new DataColumn("Id", typeof(int)), 
                                                    new DataColumn("Nume", typeof(String)),
                                                    new DataColumn("Prenume", typeof(String)),
                                                    new DataColumn("CNP", typeof(String)),
                                                    new DataColumn("Varsta",typeof(int))
                                                 });

            IEnumerable<Pacient> pacienti = db.Pacients.Where(p => p.id > 0);

            if (tboxNume.Text.Length > 0)
            {
                pacienti = pacienti.Where(p => p.nume.ToLower().Contains(tboxNume.Text.ToLower()));
            }

            if (tboxPrenume.Text.Length > 0)
            {
                pacienti = pacienti.Where(p => p.prenume.ToLower().Contains(tboxPrenume.Text.ToLower()));
            }

            if (tboxCnp.Text.Length > 0)
            {
                pacienti = pacienti.Where(p => p.cnp.Contains(tboxCnp.Text));
            }

            if (tboxVarsta.Text.Length > 0)
            {
                int varsta = int.Parse(tboxVarsta.Text);

                int currYear = DateTime.Now.Year;
                int birthYear = currYear - varsta;

                pacienti = pacienti.Where(p => p.dataNasterii.Value.Year == birthYear);
            }

            DateTime now = DateTime.Now;

            foreach (Pacient pac in pacienti)
            {
                int age = now.Year - pac.dataNasterii.Value.Year;

                dt.Rows.Add(pac.id, pac.nume, pac.prenume, pac.cnp, age);
            }

            dgvSearchResults.DataSource = dt;

            dgvSearchResults.Columns["Id"].Visible = false;
        }

        private void tboxNume_TextChanged(object sender, EventArgs e)
        {
            filterResults();
        }

        private void dgvSearchResults_DoubleClick(object sender, EventArgs e)
        {
            int id = (int)dgvSearchResults.SelectedRows[0].Cells["Id"].Value;

            this.Controls.Clear();
            this.Controls.Add(new PacientEditPanel(id));
        }

        private void tboxCnp_TextChanged(object sender, EventArgs e)
        {
            filterResults();
        }

        private void tboxPrenume_TextChanged(object sender, EventArgs e)
        {
            filterResults();
        }

        private void tboxVarsta_TextChanged(object sender, EventArgs e)
        {
            filterResults();
        }

    }
}
